$(function () {
    /**
     查询部件名称
     **/
    function getParaName() {
        $.ajax({
            // url: '../json/getParName.json',
            url: 'http://localhost:8080/getParaName',
            type: 'get',
            // dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
            // data: {method: 'getParName'},
        })
            .done(function (data) {
                console.log("getParName");
                var data = data.data;
                var html = "";
                for (var i = 0; i < data.length; i++) {
                    var parameterName = data[i].parameterName;
                    html += ` <tr>
                            <td>${data[i].processName}</td>
                            <td>${parameterName[0]}</td>
                            <td></td>
                            <td class="f_cA"></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                        </tr>`;

                    for (var j = 1; j < parameterName.length; j++) {
                        html += ` <tr>
                            <td></td>
                            <td>${parameterName[j]}</td>
                            <td></td>
                            <td class="f_cA"></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                        </tr>`;
                    }
                }

                $(html).appendTo('#table1')
            })
            .fail(function () {
                console.log("error");
            })
    }

    getParaName();

    /**
     查询部件名称
     **/
    function getCompName() {
        $.ajax({
            // url: '../json/getComName.json',
            url: 'http://localhost:8080/getCompName',
            type: 'get',
            // dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
            // data: {method: 'getComName'},
        })
            .done(function (data) {
                console.log("getCompName");
                var data = data.data;
                var html = "";

                for (var i = 0; i < data.length; i++) {
                    html += `<tr>
                            <td>${data[i]}</td>
                            <td></td>
                            </tr>`;
                }

                $(html).appendTo('#table2');
            })
            .fail(function () {
                console.log("error");
            })
    }

    getCompName();


    /**
     查询产品生产明细
     **/
    function getProInfo(productCode) {
        $.ajax({
            // url: '../json/getProInfo.json',
            url: 'http://localhost:8080/getProInfo',
            type: 'get',
            // dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
            data: {
                // method: 'getComName',
                productCode: productCode
            },
        })
            .done(function (data) {
                console.log("getProInfo");
                var data = data.data;
                var proInfo = data.proInfo;
                var componInfo = data.componInfo;
                var html = "";
                for (var i = 0; i < proInfo.length; i++) {
                    var children = proInfo[i].children;
                    if(children.length!=0){
                        html += ` <tr>
                            <td>${proInfo[i].processName}</td>
                            <td>${children[0].parameterName}</td>
                            <td>${children[0].value}</td>`;

                        if(children[0].qualifiedMark==1){
                            html+=`<td class="f_cA">合格</td>`;
                        }else if(children[0].qualifiedMark==0){
                            html+=`<td class="f_cB">不合格</td>`;
                        }
                        html+=`<td>${children[0].productionTime}</td>
                            <td>${children[0].productlineNum}</td>
                            <td>${children[0].deviceNum}</td>
                            <td>${children[0].stationNum}</td>
                            <td>${children[0].employeeCode}</td>
                        </tr>`;
                        for (var j = 1; j < children.length; j++) {
                            html += ` <tr>
                            <td></td>
                            <td>${children[j].parameterName}</td>
                            <td>${children[j].value}</td>`;
                            if(children[j].qualifiedMark==1){
                                html+=`<td class="f_cA">合格</td>`;
                            }else if(children[j].qualifiedMark==0){
                                html+=`<td class="f_cB">不合格</td>`;
                            }

                            html+=`<td>${children[j].productionTime}</td>
                            <td>${children[j].productlineNum}</td>
                            <td>${children[j].deviceNum}</td>
                            <td>${children[j].stationNum}</td>
                            <td>${children[j].employeeCode}</td>
                        </tr>`;
                        }
                        // console.log(html)
                        $(html).appendTo('#table1')
                    }
                    // else {
                    //     html+=`<tr>
                    //         <td>${proInfo[i].processName}</td></tr>`;
                    //     $(html).appendTo('#table1')
                    // }
                }

                var html2 = "";

                for (var i = 0; i < componInfo.length; i++) {
                    html2 += `<tr>
                            <td>${componInfo[i].componentName}</td>
                            <td>${componInfo[i].batch}</td>
                            </tr>`;
                }

                $(html2).appendTo('#table2');
                // console.log(data);

                $("#batch").val(data.batch)
            })
            .fail(function () {
                console.log("error");
            })
    }

    $("#getProInfo").click(function (event) {
        /* Act on the event */
        // console.log("1");
        $("#table1 tr:not(:first-child)").empty();
        $("#table2 tr:not(:first-child)").empty();
        getProInfo($("#productCode").val());
    });


    $(".openBad").click(function (event) {
        /* Act on the event */

        layer.open({
            type: 2,
            title: "",
            area: ['800px', '400px'],
            content: "openBad.html"
        });
    });

})